// by Xeno
#define __DEBUG__
#define THIS_FILE "fn_target_clear.sqf"
#include "x_setup.sqf"
private ["_current_target_pos","_dummy","_ran","_start_real"];
if (!isServer) exitWith{};

sleep 1.123;

if (!isNull GVAR(f_check_trigger)) then {deleteVehicle GVAR(f_check_trigger)};
#ifdef __TT__
if (!isNull GVAR(f_check_trigger2)) then {deleteVehicle GVAR(f_check_trigger2)};
#endif
deleteVehicle GVAR(current_trigger);
if (!isNil QGVAR(HC_CLIENT_OBJ)) then {
	[QGVAR(xdelct), GVAR(HC_CLIENT_OBJ)] call FUNC(NetCallEventSTO);
};
// TYRGHEN - Cleaning up of priority stuff
if (!isNull GVAR(priority_trigger)) then {deleteVehicle GVAR(priority_trigger)};
sleep 0.01;

__TargetInfo

#ifndef __TT__
GVAR(counterattack) = false;
_start_real = false;
_ran = random 100;
if (_ran > 96) then {
	GVAR(counterattack) = true;
	_start_real = true;	
	GVAR(kb_logic1) kbTell [GVAR(kb_logic2),GVAR(kb_topic_side),"CounterattackEnemy",["1","",_current_target_name,[]],"SIDE"];
	0 spawn FUNC(counterattack);
};

while {GVAR(counterattack)} do {sleep 3.123};

if (_start_real) then {
	GVAR(kb_logic1) kbTell [GVAR(kb_logic2),GVAR(kb_topic_side),"CounterattackDefeated","SIDE"];
	sleep 2.321;
};
#endif

GVAR(old_target_pos) =+ ((GVAR(target_names) select __XJIPGetVar(GVAR(current_target_index))) select 0);
GVAR(old_radius) = [(GVAR(target_names) select __XJIPGetVar(GVAR(current_target_index))) select 2];

#ifndef __TT__
_resolved_targets = __XJIPGetVar(resolved_targets);
_resolved_targets set [count _resolved_targets, __XJIPGetVar(GVAR(current_target_index))];
["resolved_targets",_resolved_targets] call FUNC(NetSetJIP);
__TRACE_1("","_resolved_targets");
#endif

#ifdef __TT__
private ["_objects", "_wf", "_addpblufor", "_addpopfor"];
_objects = _wf nearEntities [["CAManBase", "Tank", "Car"], 12];
_addpblufor = 0;
_addpopfor = 0;
if (count _objects > 0) then {
	{
		if (alive _x) then {
			if (vehicle _x == _x) then {
				if (isPlayer _x) then {
					_x addScore (GVAR(tt_points) select 0);
					switch (side (group _x)) do {
						case blufor: {_addpblufor = _addpblufor + (GVAR(tt_points) select 0)};
						case opfor: {_addpopfor = _addpopfor + (GVAR(tt_points) select 0)};
					};
				};
			} else {
				{
					if (alive _x) then {
						if (isPlayer _x) then {
							_x addScore (GVAR(tt_points) select 0);
							switch (side (group _x)) do {
								case blufor: {_addpblufor = _addpblufor + (GVAR(tt_points) select 0)};
								case opfor: {_addpopfor = _addpopfor + (GVAR(tt_points) select 0)};
							};
						};
					};
				} forEach (crew _x);
			};
		};
	} forEach _objects;
};

GVAR(kill_points_blufor) = GVAR(kill_points_blufor) + _addpblufor;
GVAR(kill_points_opfor) = GVAR(kill_points_opfor) + _addpopfor;

if (GVAR(kill_points_blufor) > GVAR(kill_points_opfor)) then {
	GVAR(mt_winner) = 1;
	GVAR(points_blufor) = GVAR(points_blufor) + (GVAR(tt_points) select 0);
} else {
	if (GVAR(kill_points_opfor) > GVAR(kill_points_blufor)) then {
		GVAR(mt_winner) = 2;
		GVAR(points_opfor) = GVAR(points_opfor) + (GVAR(tt_points) select 0);
	} else {
		if (GVAR(kill_points_opfor) == GVAR(kill_points_blufor)) then {
			GVAR(mt_winner) = 3;
			GVAR(points_blufor) = GVAR(points_blufor) + (GVAR(tt_points) select 1);
			GVAR(points_opfor) = GVAR(points_opfor) + (GVAR(tt_points) select 1);
		};
	};
};
["points_array",[GVAR(points_blufor),GVAR(points_opfor),GVAR(kill_points_blufor),GVAR(kill_points_opfor)]] call FUNC(NetSetJIP);
_resolved_targets = __XJIPGetVar(resolved_targets);
_resolved_targets set [count _resolved_targets, [__XJIPGetVar(GVAR(current_target_index)),GVAR(mt_winner)]];
["resolved_targets",_resolved_targets] call FUNC(NetSetJIP);
[QGVAR(mt_winner), GVAR(mt_winner)] call FUNC(NetCallEventToClients);

sleep 0.5;
GVAR(public_points) = false;
#endif

if (!isNil QGVAR(HC_CLIENT_OBJ)) then {
	[QGVAR(dodelintelu), GVAR(HC_CLIENT_OBJ)] call FUNC(NetCallEventSTO);
} else {
	if (!isNull GVAR(intel_unit)) then {
		deleteVehicle GVAR(intel_unit);
		GVAR(intel_unit) = objNull;
	};
};

sleep 0.5;

if (GVAR(current_counter) < GVAR(MainTargets)) then {
	0 spawn FUNC(gettargetbonus);
} else {
	["target_clear",true] call FUNC(NetSetJIP);
#ifndef __TT__
	[QGVAR(target_clear), ""] call FUNC(NetCallEventToClients);
#else
	[QGVAR(target_clear), ["",""]] call FUNC(NetCallEventToClients);
#endif
	_tname = _current_target_name call FUNC(KBUseName);
#ifndef __TT__
	GVAR(kb_logic1) kbTell [GVAR(kb_logic2),GVAR(kb_topic_side),"Captured2",["1","",_current_target_name,[_tname]],"SIDE"];
#else
	GVAR(hq_logic_en1) kbTell [GVAR(hq_logic_en2),"HQ_W","Captured2",["1","",_current_target_name,[_tname]],"SIDE"];
	GVAR(hq_logic_ru1) kbTell [GVAR(hq_logic_ru2),"HQ_E","Captured2",["1","",_current_target_name,[_tname]],"SIDE"];
#endif
};

sleep 2.123;

if (!isNil QGVAR(HC_CLIENT_OBJ)) then {
	[QGVAR(dodel1), [GVAR(HC_CLIENT_OBJ), __XJIPGetVar(GVAR(current_target_index))]] call FUNC(NetCallEventSTO);
} else {
	__XJIPGetVar(GVAR(current_target_index)) execFSM "fsms\DeleteUnits.fsm";
	if (!isNil QGVAR(mines_created) && {count GVAR(mines_created) > 0}) then {
		{
			deleteMarker format ["mines_",_x];
			deleteVehicle _x;
		} forEach GVAR(mines_created);
		GVAR(mines_created) = [];
	};
};

sleep 4.321;

#ifndef __TT__
if (GVAR(WithJumpFlags) == 1) then {
	if (GVAR(current_counter) < GVAR(MainTargets)) then {0 spawn FUNC(createjumpflag)};
};
#endif

if (!isNil QGVAR(HC_CLIENT_OBJ)) then {
	[QGVAR(dodelrspgrps), GVAR(HC_CLIENT_OBJ)] call FUNC(NetCallEventSTO);
} else {
	if (count GVAR(respawn_ai_groups) > 0) then {
		0 spawn {
			__TRACE_1("","d_respawn_ai_groups");
			{
				if (typeName _x == "ARRAY") then {
					private "_rgrp";
					_rgrp = _x select 0;
					__TRACE_1("","_rgrp");
					if (!isNil "_rgrp" && {typeName _rgrp == "GROUP"} && {!isNull _rgrp}) then {
						{
							private "_uni";
							_uni = _x;
							__TRACE_1("","_uni");
							if (!isNil "_uni" && {!isNull _uni}) then {
								private "_v";
								_v = vehicle _uni;
								__TRACE_1("","_v");
								if (_v != _uni && {alive _v}) then {_v setDamage 1};
								if (alive _uni) then {_uni setDamage 1}
							};
						} forEach (units _rgrp);
					};
				};
			} forEach GVAR(respawn_ai_groups);
		};
	};
};

GVAR(del_camps_stuff) = [];
{
	_flag = _x getVariable QGVAR(FLAG);
	_mar = format [QGVAR(camp%1),_x getVariable QGVAR(INDEX)];
	deleteMarker _mar;
	GVAR(del_camps_stuff) set [count GVAR(del_camps_stuff), _x];
	if (!isNull _flag) then {
		GVAR(del_camps_stuff) set [count GVAR(del_camps_stuff), _flag];
	};
} forEach __XJIPGetVar(GVAR(currentcamps));
[QGVAR(currentcamps),[]] call FUNC(NetSetJIP);
[QGVAR(campscaptured_w),0] call FUNC(NetSetJIP);
[QGVAR(campscaptured_e),0] call FUNC(NetSetJIP);

sleep 0.245;

if (GVAR(delete_mt_vehicles_after_time) != 0) then {
	if (isNil QGVAR(HC_CLIENT_OBJ)) then {
		[GVAR(old_target_pos),GVAR(old_radius)] execFSM "fsms\DeleteEmpty.fsm";
	} else {
		[QGVAR(dodel2), [GVAR(HC_CLIENT_OBJ), [GVAR(old_target_pos), GVAR(old_radius), GVAR(del_camps_stuff)]]] call FUNC(NetCallEventSTO);
		GVAR(del_camps_stuff) = nil;
	};
};

GVAR(run_illum) = false;

if (GVAR(current_counter) < GVAR(MainTargets)) then {
	if (GVAR(MHQDisableNearMT) != 0) then {
		{
			if (alive _x) then {
				_fux = GV(_x,GVAR(vecfuelmhq));
				if (!isNil "_fux") then {
					if (fuel _x < _fux) then {
						[QGVAR(setFuel), [_x, _fux]] call FUNC(NetCallEventSTO);
					};
					_x setVariable [QGVAR(vecfuelmhq), nil, true];
				};
			};
		} forEach vehicles;
	};
	sleep 15;
#ifdef __TT__
	GVAR(kill_points_blufor) = 0;
	GVAR(kill_points_opfor) = 0;
	GVAR(public_points) = true;
#endif
	0 spawn FUNC(createnexttarget);
} else {
#ifdef __TT__
	[QGVAR(the_end),true] call FUNC(NetSetJIP);
	0 spawn FUNC(DomEnd);
#else
	if (GVAR(WithRecapture) == 0) then {
		if (count GVAR(recapture_indices) == 0) then {
			[QGVAR(the_end),true] call FUNC(NetSetJIP);
			0 spawn FUNC(DomEnd);
		} else {
			0 spawn {
				scriptName "spawn_x_target_clear_waitforrecap";
				while {count GVAR(recapture_indices) > 0} do {
					sleep 2.543;
				};
				[QGVAR(the_end),true] call FUNC(NetSetJIP);
				0 spawn FUNC(DomEnd);
			};
		};
	} else {
		[QGVAR(the_end),true] call FUNC(NetSetJIP);
		0 spawn FUNC(DomEnd);
	};
#endif
};